package logic

import (
	"context"
	"login-hub/model/mysql"

	"login-hub/loginrpc/internal/svc"
	"login-hub/loginrpc/loginrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type GoodListLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewGoodListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GoodListLogic {
	return &GoodListLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *GoodListLogic) GoodList(in *loginrpc.GoodListRequest) (*loginrpc.GoodListResponse, error) {
	// todo: add your logic here and delete this line

	var good []mysql.Good
	mysql.DB.Debug().Scopes(mysql.Paginate(int(in.Page), int(in.Size))).Find(&good)

	var goodList []*loginrpc.GoodList

	for _, goods := range good {

		list := loginrpc.GoodList{
			Name:  goods.Name,
			Price: float32(goods.Price),
			Uid:   int64(goods.Uid),
		}
		goodList = append(goodList, &list)
	}

	return &loginrpc.GoodListResponse{
		List: goodList,
	}, nil
}
